#!/bin/bash
declare -A testSets
testSets["arrayAccess"]="./ts-swift-workload/array-access/swift/"
testSets["fannkuc-redux"]="./ts-swift-workload/array-access/swift/"
testSets["fastarepeat"]="./ts-swift-workload/array-access/swift/"
testSets["fastarandom1"]="./ts-swift-workload/array-access/swift/"
testSets["fastarandom2"]="./ts-swift-workload/array-access/swift/"
testSets["mandelbrot"]="./ts-swift-workload/array-access/swift/"
testSets["spectralnorm"]="./ts-swift-workload/array-access/swift/"
testSets["threeDCube"]="./ts-swift-workload/array-access/swift/"
testSets["threeDRaytrace"]="./ts-swift-workload/array-access/swift/"
testSets["Method_Call"]="./ts-swift-workload/function-call/swift/"
testSets["Normal_Call"]="./ts-swift-workload/function-call/swift/"
testSets["Pointer_Call"]="./ts-swift-workload/function-call/swift/"
testSets["StaticFunc_Call"]="./ts-swift-workload/function-call/swift/"
testSets["nbody"]="./ts-swift-workload/nbody/swift/"
testSets["cocos"]="./ts-swift-workload/cocos/swift/"
testSets["cocos_airplane"]="./ts-swift-workload/cocos/swift/"
testSets["bitops"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["cordic"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["numericalComputation"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["binarySearchFloat"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["binarySearchInteger"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["partialSum"]="./ts-swift-workload/numerical-calculation/swift/"
testSets["propertyaccess2"]="./ts-swift-workload/property-access/swift/"
testSets["splay"]="./ts-swift-workload/splay/swift/"

arg_name=""
true="true"
false="false"
run_count=1
date=$(date +'%Y%m%d%H%M%S')

function help_explain(){
    echo "[--help,--h]"
    echo "[--date <date>]"
    echo "[--run-count <count>]"
    echo "[--case <case-name>]"
    echo "[--build]"
    echo "[--excel]"
}

if [ -z "$1" ]; then
    echo "No input provided."
else
    for arg in "$@"; do
        case $arg_name in
            "--case")
                arg_name=""
                case_name=$arg
                ;;
            "--date")
                arg_name=""
                date=$arg
                ;;
            "--run-count")
                arg_name=""
                run_count=$arg
                ;;
        esac
        case $arg in
            "--h")
                help_explain
                ;;
            "--help")
                help_explain
                ;;
            "--build")
                build="true"
                ;;
            "--case")
                arg_name=$arg
                ;;
            "--date")
                arg_name=$arg
                ;;
            "--excel")
                excel="true"
                ;;
            "--run-count")
                arg_name=$arg
                ;;
        esac
    done
fi

swift2path="swift_build/${date}"
function build_swift(){
    local swift_file=$1$2.swift
    local file_name=$2
    echo ${file_name}
    swiftc -O -whole-module-optimization ${swift_file} -o ${swift2path}/${file_name}
}

function run_swift(){
    local file_name=$1
    local swift_file=$swift2path/${file_name}
    echo ${file_name}
    $swift_file
}

if [[ "$build" = "true" ]]; then
    if [ ! -d $swift2path ];then
        mkdir -p $swift2path
    fi
    if [ -z "$case_name" ]; then
        for key in ${!testSets[@]}; do
            if [ -n "${testSets[${key}]}" ]; then
                build_swift ${testSets[${key}]} ${key} 
            else
                echo "${key} does't exist in cases"
            fi
        done
    else
        if [ -n "${testSets[${case_name}]}" ]; then
            build_swift ${testSets[${case_name}]} ${case_name}
        else
            echo "${case_name} does't exist in cases"
        fi
    fi  
fi

if [ ! -d $swift2path ];then
    echo "${swift2path} No such file or directory"
    exit
fi

if [[ "$excel" = "true" ]]; then
    if [ -z "$case_name" ]; then
        for key in ${!testSets[@]}; do
            python swift2excel.py --dir $swift2path --run-case $key --run-count $run_count
        done
    else
        python swift2excel.py --dir $swift2path --run-case $case_name --run-count $run_count
    fi
fi
